﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Zxb.DayWork.Core;

namespace Zxb.DayWork.Logic
{
    public class UpCaseSearchViewNumHit
    {
        public static void Run()
        {
            //SqlConnection sqlConn = null;
            //try
            //{
            //    sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ZxbWriter"].ConnectionString);
            //    DataTable dt = SetCaseSearchViewNumHit();
            //    SqlCommand cmd = new SqlCommand("UpCaseSearchViewNumHit", sqlConn);

            //    cmd.CommandType = CommandType.StoredProcedure;
            //    SqlParameter tvpParam = cmd.Parameters.AddWithValue("@tvpCSVNH", dt);
            //    tvpParam.SqlDbType = SqlDbType.Structured;
            //    tvpParam.TypeName = "UdtCaseSearchViewNumHits";

            //    sqlConn.Open();

            //    if (dt != null && dt.Rows.Count != 0)
            //    {
            //        SqlDataAdapter da = new SqlDataAdapter(cmd);
            //        DataSet ds = new DataSet();
            //        da.Fill(ds);
            //        DataTable ddd = ds.Tables[0];
            //        //cmd.ExecuteNonQuery();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Logger.InfoLog.Error("更新楼盘案例搜索显示时的总案例个数及点击量时出错:" + ex.Message);
            //}
            //finally
            //{
            //    if (sqlConn != null)
            //    {
            //        sqlConn.Close();
            //    }
            //}
        }

        private static DataTable GetCase()
        {
            try
            {
                Database db = DB.TaskReader;
                string selSql = "select RealEstateID, CaseID, hits,CityID from cases with(nolock)";
                DbCommand Cmd = db.GetSqlStringCommand(selSql.ToString());
                return db.ExecuteDataSet(Cmd).Tables[0];
            }
            catch (Exception ex)
            {
                Logger.InfoLog.Error("抽取240楼盘案例基本数据时出错:" + ex.Message);
                return null;
            }
        }

        private static DataTable GetEstateCaseNum()
        {
            try
            {
                Database db = DB.TaskReader;
                string selSql = "select distinct RealEstateID, COUNT(CaseID) over(partition by RealEstateID) caseNum ,SUM(hits) over(partition by RealEstateID) hitsNum,CityID from cases with(nolock)";
                DbCommand Cmd = db.GetSqlStringCommand(selSql.ToString());
                return db.ExecuteDataSet(Cmd).Tables[0];
            }
            catch (Exception ex)
            {
                Logger.InfoLog.Error("抽取240楼盘案例基本数据时出错:" + ex.Message);
                return null;
            }
        }

        private static DataTable SetCaseSearchViewNumHit()
        {
            DataTable dt1 = GetCase();
            DataTable dt2 = GetEstateCaseNum();

            int loupanCnt = dt2.Rows.Count;

            for(int i=0;i<loupanCnt;i++)
            {
                foreach (DataRow dr in dt1.Rows)
                {
                    if (dr["CityID"].ToString() != dt1.Rows[i]["CityID"].ToString()
                        && (int.Parse(dr["CaseID"].ToString()) ^ int.Parse(dt1.Rows[i]["CityID"].ToString())) % 4 == 0
                        && (int.Parse(dr["CaseID"].ToString()) ^ long.Parse(dt1.Rows[i]["RealEstateID"].ToString())) % 73 == 0)
                    {
                        dt2.Rows[i]["caseNum"] = int.Parse(dt2.Rows[i]["caseNum"].ToString()) + 1;
                        dt2.Rows[i]["hitsNum"] = int.Parse(dt2.Rows[i]["hitsNum"].ToString()) + int.Parse(dr["hits"].ToString());
                    }
                }
            }

            return dt2;
        }
    }
}
